const app = getApp();
const Controller = require('../controllers/controllers.js').controller;

Page({
  data: {
    hasMobile: false, //没有手机号
    userInfo:''
  },
  onLoad(options) {
    if(!wx.getStorageSync('logintokenlinshi')){
      app.GetLogin()
    }
    if((wx.getStorageSync('wxuserInfo') && wx.getStorageSync('wxuserInfo').nickName && wx.getStorageSync('wxuserInfo').nickName!='微信用户') || (wx.getStorageSync('userInfo') && wx.getStorageSync('userInfo').nick && wx.getStorageSync('userInfo').nick!='微信用户')){ 
      this.setData({
        userInfo:wx.getStorageSync('userInfo') || wx.getStorageSync('wxuserInfo')
      })
    }else{
      this.bindgetuserinfoBtn()
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  goagree(){
    wx.navigateTo({
      url: "/pages/doc/doc?type=key-agreement-content"
    })
  },
  bindgetuserinfoBtn(e) {
    var that=this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        if(res.userInfo){
          wx.setStorageSync('wxuserInfo', res.userInfo)
          that.updateUserInfo(res).then(res => {
            that.setData({
              hasMobile:true
            })
          })
        }
      },
      fail:(res)=>{
 
      }
    })
  },
  // 手机授权
  bindgetphonenumber(e) {
    console.log(e)
    if(e.detail && e.detail.errMsg!='getPhoneNumber:fail user deny'){
      app.updatePhoneNumber(e.detail).then(res => {
        wx.showToast({
          title: '登录成功！',
          icon: 'success',
          duration: 2000
        })
        var logintoken=wx.getStorageSync('logintokenlinshi')
        wx.setStorageSync('logintoken',logintoken)
        var that=this;
        setTimeout(res => {
          wx.navigateBack({ //返回
            delta: 1
          })
          // that.interest()
        },2000)
      })
    }
  },
  interest(){
    Controller.checkCollectInfo().then(res=>{
      if(res.data.baseData==1){
          wx.navigateBack({ //返回
            delta: 1
          })
      }else{
        wx.navigateTo({
          url: "/pages/interest/interest"
        })
      }
      
    })
  },
  other(){
    wx.navigateTo({
      url: "/pages/Login/other"
    })
  },
})